<?php
class	Custom_Controller_DefaultController	extends	Custom_Controller_RootController
{
	
	//初始化设置
	public function init()
	{	
		$this->view->BaseUrl=$this->_request->getBaseUrl();
		$this->view->module=$this->_request->getModuleName();
		$this->view->controller=$this->_request->getControllerName();
		$this->view->action=$this->_request->getActionName();
       
        //根据不同的用户取不同Layout
        $this->view->template='default';
		$this->view->addScriptPath(PROJECT_ROOT.'/application/modules/default/views/scripts/'.$this->view->template.'/');
		$this->_helper->layout()->setLayout($this->view->template);
		
		$uid = (int)$this->_request->getParam('uid');	
		$uid = 1;
		$this->view->uid=$this->_uid= $uid;
		$member=new Member();
		
		if(empty($uid)||(!$member->fetchRow('uid='.$uid))){
			$this->showMessage('用户不存在或非法操作!');
			exit;
		}
		
		
		//检测用户是否登陆
		/*
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$this->_authInfo = Zend_Auth::getInstance()->getIdentity();
			$this->view->authUid=$this->authUid=$this->_authInfo->uid;
		}
		*/
		
		$infoArray=$member->getMemberById($this->_uid);
		
		$this->view->companyname=($infoArray['company'])?$infoArray['company']:'请设置公司名称,并编辑.';
		$this->view->logo=($infoArray['avatar'])?$infoArray['avatar']:'public/images/no_image.gif';
		$space_config=$infoArray['spaceconfig'];
		$this->_helper->layout()->setLayout('default');
		
		//用户空间设置
		$spacesetting = array();
		/*
		if($space_config&&$tmp=unserialize($space_config)){
			$spacesetting = $tmp;
			
			unset($tmp);
		}else{
			*/
			$spacesetting = array(
				'bannerurl' => '',
				'skin' => 5,
				'style' => 2,
				'frame' => 1,
				'template' => 2,
				'contentclass' => 'w950 l2-230',
				'blogname' => '',
				'css' => array('logoCss' => '#header .logo{top:68px; left:29px;}',
				            'companyNameCss' => '#header h1{cursor: move; left: 52px; top: 75px; color: rgb(0, 0, 0);}',
				            'sloganCss' => '',
				            'searchCss' => '#header .search_area{cursor: move; left: 635px; top: 108px; color: rgb(0, 0, 0);}'
				),
				'left' => array( 'about','newsclass','link'),
				'center' => array('news'),
				'right' => array(),
				'modules' => array('about','newsclass','link','news'
				),
				'menus' => array( 
					'0' => Array(
	                    'name' => '产品展示',
	                    'link' => 'product',
	                    'code' => 'product',
	                    'order' => 2
	                ),
	                '1' => Array
	                (
	                    'name' => '公司简介',
	                    'link' => 'about',
	                    'code' => 'about',
	                    'order' => 2
	                ),
	                '2' => Array
	                (
	                    'name' => '企业新闻',
	                    'link' => 'news',
	                    'code' => 'news',
	                    'order' => 4
	                ),
	                '3' => Array
	                (
	                    'name' => '客户留言',
	                    'link' => 'guestbook',
	                    'code' => 'guestbook',
	                    'order' => 12
	                ),
	                '4' => Array
	                (
	                    'name' => '联系我们',
	                    'link' => 'contact',
	                    'code' => 'contact',
	                    'order' => 13
	                )
	             )				
			);
		//}
		$nonExistsMenus = array();
		$userMenuCode = array();
		if(!(empty($spacesetting['menus'])) && is_array($spacesetting['menus'])){
			foreach ($spacesetting['menus'] as $m){
				$userMenuCode[] = $m['code'];				
			}
		}
		
		//系统默认菜单
		$oModules = new Modules();
		$menurows = $oModules->getMenus();
		
		$sysmenus = array();		
		foreach ($menurows as $m){					
			$row = array(				
				'name'=>$m['name'], 
				'link'=>$m['field'],			
				'code'=>$m['field'],
				'order'=>$m['order'],	
			);
			$sysmenus[] = $row;
			if(!in_array($m['field'],$userMenuCode)){
				$nonExistsMenus[] = $row;								
			}
		}
		
		
		//系统模块
		$sysmodules = $oModules->getModules();
		
		$sysmodulesIdKey = array();
		$sysmodulesCodeKey = array();
		$menuname = array();
		$isEmpty = empty($spacesetting['modules'])?true:false;
		foreach ($sysmodules as $m){
			$sysmodulesIdKey[$m['id']] = $m;
			$sysmodulesCodeKey[$m['field']] = $m;
			$menuname[$m['field']] =$m['name'];
			if($isEmpty){
				if($m['position'] == 'center'){
					$spacesetting['center'][] = $m['field'];
				}elseif ($m['position'] == 'right'){
					$spacesetting['right'][] = $m['field'];
				}else {
					$spacesetting['left'][] = $m['field'];
				}
			}			
		}
		$this->_spacesetting = $spacesetting;		
		
		foreach ($spacesetting['menus'] as $mv){
			$menuname[$mv['code']] = $mv['name'];
		}
		
		$this->view->menuname = $menuname;
		
		//空间设置－banner
		if($this->_request->getControllerName() == 'index'){
			$banners = array();
			$sbanners = glob(PROJECT_ROOT.'/public/'.$this->view->module.'/banner/s/*.jpg');
			if(is_array($sbanners))foreach ($sbanners as $b){
				$banners[] = basename($b);
			}
			$this->view->banners = $banners;		
		}
	
	
		//设置视图中的变量
		$this->view->userMenus = $spacesetting['menus'];	
		$this->view->nonExistsMenus = $nonExistsMenus;
		$this->view->sysmodules = $sysmodules;
		$this->view->sysmoduleIdKeys = $sysmodulesCodeKey;
		$this->view->sysmoduleCodeKeys = $sysmodulesCodeKey;
		$this->view->spacesetting = $spacesetting;
		$this->view->currentControler = $this->_request->getControllerName();
		$this->view->banners = $banners;
		
		//初始化父类
		parent::init();
	}
	
	function preDispatch(){
		
	}

	/**
	 * 设置消息
	 *
	 * @param string $msg
	 */
	protected function setMessage($message)
	{
		$this->_helper->flashMessenger->setNamespace('spacemessage');
		$this->_helper->flashMessenger($message);
		
	}
	
	/**
	 * 取得消息
	 *
	 * @return array
	 */
	protected function getMessage()
	{
		
		$this->_helper->flashMessenger->setNamespace('spacemessage');
		$message = $this->_helper->flashMessenger->getMessages();
		
		return $message;		
	}
	
	
	/**
	 * 
	 * @param string $message
	 * @param string $url
	 */
	protected function showMessage($message, $url ='')
	{
		$this->setMessage($message);		
		$this->setMessage($url);
		$this->_redirect($this->view->urls('index','message','default',array()));
	}
}